package menuMaster;

import java.awt.GridLayout;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

/************************************************
 * 
 * @author MenuGroup
 *  
 *  The Build class takes user input to create a
 *  new XML toolbar which is parsable by CookSwing.
 *  The name of the toolbar is written to
 *  toolbarlist.dat, which CookSwing will read from
 *  to generate a new toolbar. 
 *  
 ***********************************************/

public class Build extends JFrame implements ActionListener{

	private static final long serialVersionUID = 3694487660466072388L;
	
	private JButton addItem, create;
	private JMenuItem actionItem;
	
	/************************************************************
	 * 
	 * menuItemTypes is a list of strings associated with the
	 * current CookSwing tags supported by the menu builder.
	 * Any time this list is updated, the switch statement later
	 * in the code should be updated with the appropriate tag
	 *
	 ***********************************************************/
	
	private String[] menuItemTypes = {"Button", "Check Box Menu Item", "Text Field",
	                                  "Drop-down Menu", "Toolbar Separator"};
	
	private JTextField textField = new JTextField("", 30);
	private JTextField handlerField = new JTextField("", 30); 
	private JTextField fileField = new JTextField("newbar.xml", 30); 
	private JComboBox typeField = new JComboBox(menuItemTypes);
	private DefaultListModel model = new DefaultListModel();
	private JList itemList = new JList(model);
	private JScrollPane scroller = new JScrollPane(itemList);
	
	private JFrame frame = new JFrame();
	private String components = "";

	/**********************************************************
	 * 
	 * Constructor sets up the GUI and adds necessary action
	 * handlers to buttons used by the menu builder
	 * 
	 **********************************************************/
	
	public Build(String title) {
		this.setTitle(title);
		this.setSize(330, 400);
		this.setLocationByPlatform(true);
	
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(0,2));
		
		JLabel textLabel = new JLabel("Toolbar Item Text:");
		JLabel handlerLabel = new JLabel("Action Handler:");
		JLabel fileLabel = new JLabel("Save toolbar as:");
		JLabel typeLabel = new JLabel("Toolbar Item Type:");
		JLabel listLabel= new JLabel("Current Toolbar Items:");
	
		JMenuBar menu = new JMenuBar();
		JMenu subMenu = new JMenu("Help");
		actionItem = new JMenuItem("Predefined Action Handlers");
		subMenu.add(actionItem);
		menu.add(subMenu);
		this.setJMenuBar(menu);
		
		actionItem.addActionListener(this);
	
		addItem = new JButton("Add Item");
		addItem.addActionListener(this);
		create = new JButton("Create Toolbar");
		create.addActionListener(this);
	
		panel.add(typeLabel);
		panel.add(typeField);
		panel.add(textLabel);
		panel.add(textField);
		panel.add(handlerLabel);
		panel.add(handlerField);
		panel.add(fileLabel);
		panel.add(fileField);
		panel.add(listLabel);
		panel.add(scroller);
		panel.add(addItem);
		panel.add(create);
	
		this.add(panel);
	}
	
	public void createGUI() {
		this.setVisible(true);
	}
	
	/**********************************************************
	 * 
	 * Takes input from typeField comboBox.  Based on which
	 * selection is made, returns appropriate CookSwing tag
	 * for toolbar XML file.  This function is called every 
	 * time the "Add Item" button is clicked.
	 * 
	 *********************************************************/
	
	public String getType() {
		int typeNum = typeField.getSelectedIndex();
		String itemType = "";
		
		switch(typeNum) {
			case 0: itemType = "button"; break;
			case 1: itemType = "checkboxmenuitem"; break;
			case 2: itemType = "textfield"; break;
			case 3: itemType = "combobox"; break;
			case 4: itemType = "toolbar-separator"; break;
			default: itemType = "button"; break;
		}
		
		return itemType;
	}
	
	/**********************************************************
	 * 
	 * Takes input from all fields in menu builder GUI and
	 * updates the appropriate string that will eventually be
	 * added to the new toolbar XML file.  Also appends new
	 * element to list shown in GUI.
	 * 
	 *********************************************************/
	
	public void addToMenu() {
		String type = "\t<" + this.getType() + " ";
		String text = "text=\"" + textField.getText() + "\" ";
		String handler = "actionlistener=\"" + handlerField.getText() + "\"></" +
		                 this.getType() + ">\n";
		String components2 = type + text + handler;
		components = components + components2;
		
		JOptionPane.showMessageDialog(frame, textField.getText() + " " + this.getType() + 
				                      " added to menu.\n", "Message", JOptionPane.PLAIN_MESSAGE);
		
		model.addElement(textField.getText() + " " +this.getType());
		
		typeField.setSelectedIndex(0);
		textField.setText("");
		handlerField.setText("");
	}

	/*****************************************************************
	 * 
	 * First FileWriter wraps the string generated by addToMenu() with
	 * the appropriate CookSwing tags and creates the new XML file.
	 * 
	 * Second FileWriter appends the name given to the new toolbar XML
	 * file to "toolbarlist.dat", from which CookSwing will read the
	 * names of the files to generate toolbars from.
	 * 
	 ****************************************************************/
	
	public void createMenu() {
		try{
			FileWriter fstream = new FileWriter(fileField.getText());
			BufferedWriter out = new BufferedWriter(fstream);
		    out.write("<toolbar>\n" + components +"</toolbar>");
		    out.close();
		    }catch (Exception e){
		      System.err.println("Error: " + e.getMessage());
		    }
		    
		 try{
			 FileWriter fstream = new FileWriter("toolbarlist.dat", true);
			 BufferedWriter out = new BufferedWriter(fstream);
			 out.write(fileField.getText() + "\n");
			 out.close();
			 }catch (Exception e){
			   System.err.println("Error: " + e.getMessage());
			 }
		
		JOptionPane.showMessageDialog(frame, "New toolbar written to " + 
				                      fileField.getText() + "\nPlease restart the program.", 
				                      "Message", JOptionPane.PLAIN_MESSAGE);
		    
		fileField.setText("");
		model.clear();
	}
	
	/***********************************************************
	 * 
	 * This pop-up message is for the sake of the user.  All
	 * action handlers which contain program functionality are
	 * currently listed, and should be updated as the program
	 * continues to grow.
	 * 
	 ***********************************************************/
	
	public void showHandlers() {
		JOptionPane.showMessageDialog(frame, "Open -> openHndlr\nClose -> closeHndlr\nNew -> newHndlr\n"
				                      + "Save -> saveHndlr\nClear -> clearHndlr\nUndo -> undoHndlr\n"
				                      + "Redo -> redoHndlr\nExport -> exportHndlr\nGrid -> gridHndlr\n"
				                      + "Shape Builder -> shapeBuildHndlr\nMenu Builder -> mbHndlr",
                                      "Message", JOptionPane.PLAIN_MESSAGE);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == create) {
			createMenu();
		}
		else if (e.getSource() == addItem) {
			addToMenu();
		}
		else if (e.getSource() == actionItem) {
			showHandlers();
		}
	}
	
}
